package com.example.module_mine.login

import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.fragment.NavHostFragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.lib_common.Router
import com.example.lib_common.base.BaseActivity
import com.example.lib_common.base.BaseToolBarActivity
import com.example.lib_common.event.EventType
import com.example.lib_common.event.LiveDataBus
import com.example.lib_common.event.LoginEvent
import com.example.lib_common.observeWithLifecycle
import com.example.lib_common.state.UIState
import com.example.module_mine.R
import com.example.module_mine.databinding.ActivityLoginBinding
import com.example.module_mine.login.viewmodel.LoginViewModel
import dagger.hilt.android.AndroidEntryPoint

@Route(path = Router.MINE_LOGIN_ACTIVITY)
@AndroidEntryPoint
class LoginActivity : BaseToolBarActivity<LoginViewModel, ActivityLoginBinding>() {
    companion object{
        private const val TAG = "LoginActivity"
    }
    override val mLayoutResId: Int
        get() = R.layout.activity_login
    override val mViewModel: LoginViewModel by viewModels()


    private lateinit var mNavController:NavController //导航控制器
    override fun initView() {
        super.initView()

        mNavController = (supportFragmentManager.findFragmentById(R.id.nav_host_container) as NavHostFragment).navController
    }

    override fun initData() {
    }

    override fun initListener() {
        //订阅当前fragment流
        mViewModel.mCurrentFragment_.observeWithLifecycle(this){
            changeFragment(it)
        }
        //点击密码登陆显示账号密码登陆fragment
        mToolBar.setOnMenuItemClickListener {
            when(it.itemId){
                R.id.action ->{
                    mNavController.navigate(R.id.action_phoneLoginFragment_to_accountLoginFragment)
                }
                else ->{

                }
            }
            return@setOnMenuItemClickListener true
        }

    }

    /**
     * 切换fragment显示不同的toolbar
     */
    private fun changeFragment(fragment:String){
        mToolBar.menu.clear()
        when(fragment){
            PhoneLoginFragment::class.simpleName -> {
                mToolBar.setNavigationIcon(R.mipmap.icon_exit)
                mToolBar.inflateMenu(R.menu.menu)
                mToolBar.menu.getItem(0).title = "密码登陆"
            }
            else ->{
                mToolBar.setNavigationIcon(R.mipmap.icon_back)
            }
        }
    }

    /**
     * 登陆成功处理
     */
    fun onHandleLoginSuccess(){
        //发送全局事件
        LiveDataBus.with<LoginEvent>(EventType.LOGIN).value = LoginEvent()
        //关闭登陆页面
        finish()
    }

}